package com.bcmusic.point.service.impl;

import com.bcmusic.point.dao.UserMcLogMapper;
import com.bcmusic.point.pojo.UserMcLog;
import com.bcmusic.point.service.UserMcLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:admin
 * @Description:UserMcLog业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class UserMcLogServiceImpl implements UserMcLogService {

    @Autowired
    private UserMcLogMapper userMcLogMapper;


    /**
     * UserMcLog条件+分页查询
     * @param userMcLog 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<UserMcLog> findPage(UserMcLog userMcLog, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(userMcLog);
        //执行搜索
        return new PageInfo<UserMcLog>(userMcLogMapper.selectByExample(example));
    }

    /**
     * UserMcLog分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<UserMcLog> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<UserMcLog>(userMcLogMapper.selectAll());
    }

    /**
     * UserMcLog条件查询
     * @param userMcLog
     * @return
     */
    @Override
    public List<UserMcLog> findList(UserMcLog userMcLog){
        //构建查询条件
        Example example = createExample(userMcLog);
        //根据构建的条件查询数据
        return userMcLogMapper.selectByExample(example);
    }


    /**
     * UserMcLog构建查询对象
     * @param userMcLog
     * @return
     */
    public Example createExample(UserMcLog userMcLog){
        Example example=new Example(UserMcLog.class);
        Example.Criteria criteria = example.createCriteria();
        if(userMcLog!=null){
            // 用户MC日志id。
            if(!StringUtils.isEmpty(userMcLog.getMcLogId())){
                    criteria.andEqualTo("mcLogId",userMcLog.getMcLogId());
            }
            // 用户编号。关联user表的user_number字段。
            if(!StringUtils.isEmpty(userMcLog.getUserNumber())){
                    criteria.andEqualTo("userNumber",userMcLog.getUserNumber());
            }
            // MC通证来源/去处。00为平台自持（+），01为上传音乐奖励（+），02为按引力值比分配（+），03为维护节点奖励（+），04为节点贡献奖励（+），05为参与收益分配（-）。
            if(!StringUtils.isEmpty(userMcLog.getSource())){
                    criteria.andEqualTo("source",userMcLog.getSource());
            }
            // 变动的MC通证。
            if(!StringUtils.isEmpty(userMcLog.getChangeMc())){
                    criteria.andEqualTo("changeMc",userMcLog.getChangeMc());
            }
            // MC日志生成时间。
            if(!StringUtils.isEmpty(userMcLog.getCreateTime())){
                    criteria.andEqualTo("createTime",userMcLog.getCreateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id){
        userMcLogMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改UserMcLog
     * @param userMcLog
     */
    @Override
    public void update(UserMcLog userMcLog){
        userMcLogMapper.updateByPrimaryKey(userMcLog);
    }

    /**
     * 增加UserMcLog
     * @param userMcLog
     */
    @Override
    public void add(UserMcLog userMcLog){
        userMcLogMapper.insert(userMcLog);
    }

    /**
     * 根据ID查询UserMcLog
     * @param id
     * @return
     */
    @Override
    public UserMcLog findById(Long id){
        return  userMcLogMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询UserMcLog全部数据
     * @return
     */
    @Override
    public List<UserMcLog> findAll() {
        return userMcLogMapper.selectAll();
    }
}
